package com.yanqu.road.server.cmd.union.cross.honors;

import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.cross.CrossUnionHonorsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.S_UNION_CHANGE_FLAG_NEW, desc = "修改商会旗帜")
public class UnionModifyFlagNewCmd extends AbstractCommand {

    List<Integer> flagList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossUnionHonorsProto.CrossUnionModifyFlagReqMsg reqMsg = CrossUnionHonorsProto.CrossUnionModifyFlagReqMsg.parseFrom(packet.getBytes());
        flagList = reqMsg.getUnionFlagList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        if(flagList.size() != 3){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
        if(null == unionInfo){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        if (unionInfo.getMasterUserId() != player.getUserId()) {//权限不足
            ret = GameErrorCode.E_UNION_POSITION_NO_ENOUGH;
            return;
        }
        if (UnionConfigMgr.getUnionLv(unionInfo.getExperience()) < GameConfig.BANNER_OPEN_NEED_UNION_LEVEL){
            ret = GameErrorCode.E_UNION_LEVEL_NO_ENOUGH;
            return;
        }

        player.sendPacket(Protocol.C_UNION_CHANGE_FLAG_NEW, CrossUnionHonorsProto.CrossUnionModifyFlagReqMsg.newBuilder().addAllUnionFlag(flagList));

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_UNION_CHANGE_FLAG_NEW, CrossUnionHonorsProto.CrossUnionModifyFlagRespMsg.newBuilder().setRet(ret));
        }
    }
}
